package com.eastfair.messagecenter.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.tenant.TenantAuth;
import com.eastfair.auth.dto.RoleDTO;
import com.eastfair.auth.dto.UserAccountDto;
import com.eastfair.auth.entity.Role;
import com.eastfair.auth.vo.OrganizationVo;
import com.eastfair.auth.vo.UserAccountVo;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.exception.BizException;
import com.eastfair.database.mybatis.conditions.Wraps;
import com.eastfair.database.mybatis.conditions.query.QueryWrap;
import com.eastfair.demand.auth.OrganizationVoGroupClass;
import com.eastfair.demand.auth.UserAccountVoGroupClass;
import com.eastfair.log.point.PointUtil;
import com.eastfair.messagecenter.ContentModel;
import com.eastfair.messagecenter.callable.SendTask;
import com.eastfair.messagecenter.domain.TaskInfo;
import com.eastfair.messagecenter.dto.*;
import com.eastfair.messagecenter.entity.InnerTemplate;
import com.eastfair.messagecenter.entity.Msg;
import com.eastfair.messagecenter.entity.SmsTemplate;
import com.eastfair.messagecenter.enumeration.*;
import com.eastfair.messagecenter.exceptioncode.MessageServiceExceptionCode;
import com.eastfair.messagecenter.model.InnerContentModel;
import com.eastfair.messagecenter.model.SmsContentModel;
import com.eastfair.messagecenter.pipeline.ProcessContext;
import com.eastfair.messagecenter.pipeline.ProcessController;
import com.eastfair.messagecenter.pipeline.SendTaskModel;
import com.eastfair.messagecenter.point.ReadySendPoint;
import com.eastfair.messagecenter.service.InnerTemplateService;
import com.eastfair.messagecenter.service.MsgService;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.messagecenter.service.SmsTemplateService;
import com.eastfair.messagecenter.vo.MsgVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;

import javax.validation.Valid;


/**
 * <p>
 * 前端控制器
 * 消息表
 * </p>
 *
 * @author ligang
 * @date 2022-09-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/msg")
@Api(value = "Msg", tags = "消息表")
@PreAuth(replace = "messagecenter:msg:")
public class MsgController extends SuperController<MsgService, Long, Msg, MsgPageQuery, MsgSaveDTO, MsgUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    PointUtil pointUtil;

    @Autowired
    ProcessController processController;
    @Autowired
    SmsTemplateService smsTemplateService;

    @Autowired
    InnerTemplateService innerTemplateService;

    @Value("${com.eastfair.projectId}")
    private Long commonProjectId;

    @Override
    public void handlerResult(IPage<Msg> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<Msg> msgList = list.stream().map((map) -> {
            Msg msg = Msg.builder().build();
            //TODO 请在这里完成转换
            return msg;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(msgList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<MsgPageQuery> params) {
        MsgPageQuery pageQuery = params.getModel();
    }


    /**
     * 发送消息
     *
     * @return
     */
    @ApiOperation(value = "发送短信消息", notes = "发送消息")
    @GetMapping("/sendMsg")
    public R sendMsg() {
        ReadySendPoint readySendPoint = new ReadySendPoint();
        readySendPoint.setBusinessId("12333");
        readySendPoint.setMsgType(MsgType.NOTIFY);
        readySendPoint.setChannelType(ChannelType.SMS);
        readySendPoint.setBusinessType("test");
        readySendPoint.setMsgContentType(MsgContentType.BUSINESS);
        Set<Long> set = new HashSet<>();
        set.add(1869275011695251456L);
        readySendPoint.setSenUserIdList(set);
        List<ContentModel> contentModelList = new ArrayList<>();
        SmsContentModel smsContentModel = new SmsContentModel();
        smsContentModel.setTemplateId(2172680712341426176L);
        Map<String, Object> contentMap = new HashMap<>();
        contentMap.put("name", "ligang");
        smsContentModel.setContentMap(contentMap);
        contentModelList.add(smsContentModel);
        readySendPoint.setContentModelList(contentModelList);
        pointUtil.addBusinPointMsg(readySendPoint);
        return R.success();

    }


    /**
     * 发送消息
     *
     * @return
     */
    @ApiOperation(value = "发送短信消息", notes = "发送消息")
    @PostMapping("/sendSmsMsg")
    public R sendSmsMsg(@RequestBody SmsSendDTO smsSendDTO) {
        //        {
//            "businessId": 123456,
//                "businessType":"TAG",
//                "msgType": "NOTIFY",
//                "msgContentType": "SYSTEM",
//                "senUserIdList": [
//            1869275011695251456
//    ],
//            "smsContentModelList": [
//            {
//                "templateId": "2528050877278982144",
//                    "contentMap": {
//                "name": "李刚"
//            }
//            }
//    ]
//        }
        //并发
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        executorService.submit(() -> {
        log.info("sendSmsMsg is execute {}", JSONObject.toJSONString(smsSendDTO));
        log.info("sendSmsMsg is execute systemScene {} , projectId {}", ContextUtil.getSystemScene(), ContextUtil.getProjectId());
        if (smsSendDTO.getSubsystemId() != null) {
            ContextUtil.setSubSystemId(smsSendDTO.getSubsystemId());
        }
        if (smsSendDTO.getTeamId() != null) {
            ContextUtil.setTeamId(smsSendDTO.getTeamId());
        }

        if (StringUtils.isNotBlank(smsSendDTO.getSystemScene()) && !ContextUtil.getSystemScene().equals(smsSendDTO.getSystemScene())) {
            ContextUtil.setSystemScene(smsSendDTO.getSystemScene());
        }


        ReadySendPoint readySendPoint = new ReadySendPoint();

        readySendPoint.setOrgIdList(smsSendDTO.getOrgIdList());
        readySendPoint.setSenUserIdList(smsSendDTO.getSenUserIdList());


        if (StringUtils.isNotBlank(smsSendDTO.getBusinessId())) {
            readySendPoint.setBusinessId(smsSendDTO.getBusinessId());
        } else {
            readySendPoint.setBusinessId(UUID.randomUUID().toString().replaceAll("-", ""));
        }
        readySendPoint.setIsSendAll(smsSendDTO.getIsSendAll());
        if (smsSendDTO.getPredictSendTime() != null) {
            readySendPoint.setPredictSendTime(smsSendDTO.getPredictSendTime());
            readySendPoint.setIsSendNow(BusinessConstant.NO);
        } else {
            readySendPoint.setIsSendNow(BusinessConstant.YES);
        }
        readySendPoint.setBusinessType(smsSendDTO.getBusinessType());
        readySendPoint.setMsgType(smsSendDTO.getMsgType());
        readySendPoint.setChannelType(ChannelType.SMS);
        readySendPoint.setMsgContentType(smsSendDTO.getMsgContentType());
        readySendPoint.setSenUserIdList(smsSendDTO.getSenUserIdList());
        readySendPoint.setSendMobileList(smsSendDTO.getMobileList());
        List<ContentModel> contentModelList = new ArrayList<>();
        for (SmsContentModel smsContentModel : smsSendDTO.getSmsContentModelList()) {
            if (smsContentModel.getTemplateId() == null) {
                SmsTemplateDTO smsTemplateDTO = new SmsTemplateDTO();
                smsTemplateDTO.setTemplateCode(smsSendDTO.getBusinessType());
                smsTemplateDTO.setSystemScene(ContextUtil.getSystemScene());
                List<SmsTemplate> smsTemplates = smsTemplateService.list(smsTemplateDTO, null);
                if (CollectionUtil.isNotEmpty(smsTemplates)) {
                    smsContentModel.setTemplateId(smsTemplates.get(0).getId());
                } else {
                    throw new BizException(MessageServiceExceptionCode.SEND_TEMPLATE_ISNULL.getCode(), MessageServiceExceptionCode.SEND_TEMPLATE_ISNULL.getMsg());
                }
            }
            contentModelList.add(smsContentModel);
        }
        readySendPoint.setContentModelList(contentModelList);
//        pointUtil.addBusinPointMsg(readySendPoint);
//        baseService.readySend(readySendPoint);

        //预发送
        //封装任务model
        SendTaskModel sendTaskModel = SendTaskModel.builder()
                .readySendPoint(readySendPoint)
                .build();
        //责任链model
        ProcessContext context = ProcessContext.builder()
                .code(BusinessCode.COMMON_SEND)
                .processModel(sendTaskModel)
                .needBreak(false)
                .response(R.success()).build();
        //启动责任链
        ProcessContext process = processController.process(context);
//        });
        return R.success();
    }


    /**
     * 发送自定义短信消息
     *
     * @return
     */
    @ApiOperation(value = "发送自定义短信消息", notes = "发送自定义短信消息")
    @PostMapping("/sendCustomSmsMsg")
    public R sendCustomSmsMsg(@RequestBody SmsSendDTO smsSendDTO) {
        //        {
//            "businessId": 123456,
//                "businessType":"TAG",
//                "msgType": "NOTIFY",
//                "msgContentType": "SYSTEM",
//                "senUserIdList": [
//            1869275011695251456
//    ],
//            "smsContentModelList": [
//            {
//                "templateId": "2528050877278982144",
//                    "contentMap": {
//                "name": "李刚"
//            }
//            }
//    ]
//        }

        if (StringUtils.isBlank(smsSendDTO.getMobileStr())) {
            return R.fail("没有手机号");
        }
        Set<String> mobileList = new HashSet<>();
        for (String s : smsSendDTO.getMobileStr().split(",")) {
            if (StringUtils.isNotBlank(s)) {
                mobileList.add(s);
            }
        }
        smsSendDTO.setMobileList(mobileList);


        smsSendDTO.setBusinessType("BUSINESS_COMMON");
        smsSendDTO.setChannelType(ChannelType.SMS);
        log.info("sendCustomSmsMsg is execute {}", JSONObject.toJSONString(smsSendDTO));
        log.info("sendCustomSmsMsg is execute systemScene {} , projectId {}", ContextUtil.getSystemScene(), ContextUtil.getProjectId());
        if (smsSendDTO.getSubsystemId() != null) {
            ContextUtil.setSubSystemId(smsSendDTO.getSubsystemId());
        }
        if (smsSendDTO.getTeamId() != null) {
            ContextUtil.setTeamId(smsSendDTO.getTeamId());
        }

        if (StringUtils.isNotBlank(smsSendDTO.getSystemScene()) && !ContextUtil.getSystemScene().equals(smsSendDTO.getSystemScene())) {
            ContextUtil.setSystemScene(smsSendDTO.getSystemScene());
        }


        ReadySendPoint readySendPoint = new ReadySendPoint();

        readySendPoint.setOrgIdList(smsSendDTO.getOrgIdList());
        readySendPoint.setSenUserIdList(smsSendDTO.getSenUserIdList());


        if (StringUtils.isNotBlank(smsSendDTO.getBusinessId())) {
            readySendPoint.setBusinessId(smsSendDTO.getBusinessId());
        } else {
            readySendPoint.setBusinessId(UUID.randomUUID().toString().replaceAll("-", ""));
        }
        readySendPoint.setIsSendAll(smsSendDTO.getIsSendAll());
        if (smsSendDTO.getPredictSendTime() != null) {
            readySendPoint.setPredictSendTime(smsSendDTO.getPredictSendTime());
            readySendPoint.setIsSendNow(BusinessConstant.NO);
        } else {
            readySendPoint.setIsSendNow(BusinessConstant.YES);
        }
        readySendPoint.setBusinessType(smsSendDTO.getBusinessType());
        readySendPoint.setMsgType(smsSendDTO.getMsgType());
        readySendPoint.setChannelType(ChannelType.SMS);
        readySendPoint.setMsgContentType(smsSendDTO.getMsgContentType());
        readySendPoint.setSenUserIdList(smsSendDTO.getSenUserIdList());
        readySendPoint.setSendMobileList(smsSendDTO.getMobileList());
        List<ContentModel> contentModelList = new ArrayList<>();
        for (SmsContentModel smsContentModel : smsSendDTO.getSmsContentModelList()) {
            if (smsContentModel.getTemplateId() == null) {
                SmsTemplateDTO smsTemplateDTO = new SmsTemplateDTO();
                smsTemplateDTO.setTemplateCode(smsSendDTO.getBusinessType());
                smsTemplateDTO.setSystemScene(ContextUtil.getSystemScene());
                List<SmsTemplate> smsTemplates = smsTemplateService.list(smsTemplateDTO, null);
                if (CollectionUtil.isNotEmpty(smsTemplates)) {
                    smsContentModel.setTemplateId(smsTemplates.get(0).getId());
                } else {
                    throw new BizException(MessageServiceExceptionCode.SEND_TEMPLATE_ISNULL.getCode(), MessageServiceExceptionCode.SEND_TEMPLATE_ISNULL.getMsg());
                }
            }
            contentModelList.add(smsContentModel);
        }
        readySendPoint.setContentModelList(contentModelList);

        //预发送
        //封装任务model
        SendTaskModel sendTaskModel = SendTaskModel.builder()
                .readySendPoint(readySendPoint)
                .build();
        //责任链model
        ProcessContext context = ProcessContext.builder()
                .code(BusinessCode.COMMON_SEND)
                .processModel(sendTaskModel)
                .needBreak(false)
                .response(R.success()).build();
        //启动责任链
        ProcessContext process = processController.process(context);
        return R.success();
    }


    /**
     * 发送站内信
     *
     * @return
     */
    @ApiOperation(value = "发送站内信", notes = "发送站内信")
    @PostMapping("/sendInnerInMsg")
    public R sendInnerInMsg(@RequestBody InnerSendDTO innerSendDTO) {

        //并发
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        executorService.submit(() -> {
        log.info("执行操作");
        log.info("sendInnerInMsg is execute {}", JSONObject.toJSONString(innerSendDTO));
        log.info("sendInnerInMsg is execute systemScene {} , subSystemId {}, projectId {}", ContextUtil.getSystemScene(), ContextUtil.getProjectId(), ContextUtil.getSubSystemId());
        if (innerSendDTO.getSubsystemId() != null) {
            ContextUtil.setSubSystemId(innerSendDTO.getSubsystemId());
        }
        if (innerSendDTO.getTeamId() != null) {
            ContextUtil.setTeamId(innerSendDTO.getTeamId());
        }
        if (StringUtils.isNotBlank(innerSendDTO.getSystemScene()) && !ContextUtil.getSystemScene().equals(innerSendDTO.getSystemScene())) {
            ContextUtil.setSystemScene(innerSendDTO.getSystemScene());
        }
        ReadySendPoint readySendPoint = new ReadySendPoint();
        readySendPoint.setIsSendAll(innerSendDTO.getIsSendAll());
        readySendPoint.setOrgIdList(innerSendDTO.getOrgIdList());
        readySendPoint.setSenUserIdList(innerSendDTO.getSenUserIdList());
        if (StringUtils.isNotBlank(innerSendDTO.getBusinessId())) {
            readySendPoint.setBusinessId(innerSendDTO.getBusinessId());
        } else {
            readySendPoint.setBusinessId(UUID.randomUUID().toString().replaceAll("-", ""));
        }
        if (innerSendDTO.getPredictSendTime() != null) {
            readySendPoint.setPredictSendTime(innerSendDTO.getPredictSendTime());
            readySendPoint.setIsSendNow(BusinessConstant.NO);
        } else {
            readySendPoint.setIsSendNow(BusinessConstant.YES);
        }
        readySendPoint.setBusinessType(innerSendDTO.getBusinessType());
        readySendPoint.setMsgType(innerSendDTO.getMsgType());
        if (innerSendDTO.getMsgType() == null) {
            readySendPoint.setMsgType(MsgType.NOTIFY);
        }
        readySendPoint.setChannelType(ChannelType.INNER_IM);
        readySendPoint.setMsgContentType(innerSendDTO.getMsgContentType());
        readySendPoint.setSenUserIdList(innerSendDTO.getSenUserIdList());
        List<ContentModel> contentModelList = new ArrayList<>();
        for (InnerContentModel innerContentModel : innerSendDTO.getInnerContentModelList()) {
            if (innerContentModel.getTemplateId() == null) {
                InnerTemplateDTO innerTemplateDTO = new InnerTemplateDTO();
                innerTemplateDTO.setTemplateCode(innerSendDTO.getBusinessType());
                innerTemplateDTO.setSystemScene(ContextUtil.getSystemScene());
                List<InnerTemplate> innerTemplateList = innerTemplateService.list(innerTemplateDTO, null);
                if (CollectionUtil.isNotEmpty(innerTemplateList)) {
                    innerContentModel.setTemplateId(innerTemplateList.get(0).getId());
                } else {
                    throw new BizException(MessageServiceExceptionCode.SEND_TEMPLATE_ISNULL.getCode(), MessageServiceExceptionCode.SEND_TEMPLATE_ISNULL.getMsg());
                }
            }
            contentModelList.add(innerContentModel);
        }
        readySendPoint.setContentModelList(contentModelList);
//        pointUtil.addBusinPointMsg(readySendPoint);
//        baseService.readySend(readySendPoint);

        //预发送
        //封装任务model
        SendTaskModel sendTaskModel = SendTaskModel.builder()
                .readySendPoint(readySendPoint)
                .build();
        //责任链model
        ProcessContext context = ProcessContext.builder()
                .code(BusinessCode.COMMON_SEND)
                .processModel(sendTaskModel)
                .needBreak(false)
                .response(R.success()).build();
        //启动责任链
        ProcessContext process = processController.process(context);

//        });

//        ExecutorService executorService = Executors.newCachedThreadPool();
//        List<Future<String>> resultList = new ArrayList<Future<String>>();
//        SendTask tr = new SendTask(context, processController);
//        // 使用的submit，非excute方法
//        Future<String> future = executorService.submit(tr);
//        try {
//            String s = future.get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
//        executorService.shutdown();
        return R.success();
    }


    /**
     * 执行查询
     * <p>
     * 可以覆盖后重写查询逻辑
     *
     * @param params 分页参数
     * @return
     */
    @ApiOperation(value = "分页列表查询")
    @PostMapping(value = "/pageVo")
//    @BaseControllerInfo(voGroup = {UserAccountVoGroupClass.pageVo.class})
    @TenantAuth(enabled = true)
    public R<IPage<MsgVO>> pageVo(@RequestBody @Valid PageParams<MsgDTO> params) {
        // 处理参数
        return R.success(baseService.pageVo(params));
    }


    /**
     * 分页查询站内信消息
     * <p>
     * 可以覆盖后重写查询逻辑
     *
     * @param params 分页参
     * @return
     */
    @ApiOperation(value = "分页查询站内信消息")
    @PostMapping(value = "/innerPageVo")
    @BaseControllerInfo(voGroup = {MsgVO.innerPageVo.class})
    public R<IPage<MsgVO>> innerPageVo(@RequestBody @Valid PageParams<MsgDTO> params) {
        // 处理参数
        params.setSort("is_read");
        params.setOrder("ascending");
        MsgDTO msgDTO = params.getModel();
        if (msgDTO == null) {
            msgDTO = new MsgDTO();
        }
        msgDTO.setUserId(ContextUtil.getUserId());
        msgDTO.setSendStatus(SendStatus.SEND_SUCC);
        msgDTO.setIsInnerMsgSendSucc(BusinessConstant.YES);
        msgDTO.setIsInnerMsgSend(BusinessConstant.YES);
        params.setModel(msgDTO);
        return R.success(baseService.pageVo(params));
    }


    /**
     * 站内信未读条数
     * <p>
     * 是否有站内信未读信息
     *
     * @return
     */
    @ApiOperation(value = "站内信未读条数")
    @GetMapping(value = "/findNoReadNum")
    @BaseControllerInfo(voGroup = {MsgVO.innerPageVo.class})
    public R findNoReadNum() {
        Long userId = ContextUtil.getUserId();

        Msg msg = new Msg();
        msg.setUserId(userId);
        msg.setIsRead(BusinessConstant.NO);
        msg.setIsInnerMsgSend(BusinessConstant.YES);
        msg.setIsSendNow(BusinessConstant.YES);
        msg.setIsInnerMsgSendSucc(BusinessConstant.YES);
        QueryWrap<Msg> queryWrapper = Wraps.q(msg);
        queryWrapper.lambda()
                .eq(Msg::getIsDeleted, BusinessConstant.NO);
        int num = baseService.count(queryWrapper);
        // 处理参数
        return R.success(num);
    }

    /**
     * 站内信消息已读
     * <p>
     * 可以覆盖后重写查询逻辑
     *
     * @param msgDTO 分页参数
     * @return
     */
    @ApiOperation(value = "站内信消息已读")
    @PostMapping(value = "/innerRead")
    @BaseControllerInfo()
    public R innerRead(@RequestBody MsgDTO msgDTO) {
        List<Msg> msgList = baseService.list(null, msgDTO.getIds());
        if (CollectionUtil.isNotEmpty(msgList)) {
            for (Msg msg : msgList) {
                msg.setIsRead(BusinessConstant.YES);
                baseService.updateById(msg);
            }
        }
        return R.success();
    }


    /**
     * 定时发送
     * <p>
     *
     * @return
     */
    @ApiOperation(value = "定时发送")
    @GetMapping(value = "/executeSend")
    @TenantAuth(enabled = true)
    public R executeSend() {
        //忽略多租户
        ContextUtil.setIsSystemOlationIgnore(true);
        ContextUtil.setProjectId(commonProjectId);
        MsgDTO msgDTO = new MsgDTO();
        msgDTO.setSendStatus(SendStatus.WAIT);
        msgDTO.setPredictSendTimeOfLe(LocalDateTime.now());
        List<Msg> msgList = baseService.list(baseService.assembleQueryWrapper(msgDTO, null));
        log.info("msgList is {}", JSONObject.toJSONString(msgList));

        //或者立即发送但没有发送的
        msgDTO = new MsgDTO();
        msgDTO.setSendStatus(SendStatus.WAIT);
        msgDTO.setIsSendNow(BusinessConstant.YES);
        msgDTO.setCreateTimeOfLe(LocalDateTime.now().plusMinutes(5));
        List<Msg> msgList2 = baseService.list(baseService.assembleQueryWrapper(msgDTO, null));

        if (CollectionUtil.isNotEmpty(msgList2)) {
            msgList.addAll(msgList2);
        }
        // 处理参数
        if (CollectionUtil.isEmpty(msgList)) {
            return R.success();
        }
        List<Msg> smsMsgList = new ArrayList<>();
        List<Msg> innerMsgList = new ArrayList<>();
        for (Msg msg : msgList) {
            if (BusinessConstant.YES == msg.getIsNoteSend()) {
                smsMsgList.add(msg);
            } else if (BusinessConstant.YES == msg.getIsInnerMsgSend()) {
                innerMsgList.add(msg);
            }
        }
        //短信发送
        executeSmsSend(smsMsgList);
        //站内信发送
        executeInnerSend(innerMsgList);
        return R.success();
    }

    public void executeInnerSend(List<Msg> innerMsgList) {
        if (CollectionUtil.isNotEmpty(innerMsgList)) {
            List<TaskInfo> taskInfoList = new ArrayList<>();
            for (Msg msg : innerMsgList) {
                msg.setIsInnerMsgSendSucc(BusinessConstant.YES);
                msg.setSendTime(LocalDateTime.now());
                msg.setSendStatus(SendStatus.SEND_SUCC);
                baseService.updateById(msg);
            }
        }
    }

    public void executeSmsSend(List<Msg> smsMsgList) {
        if (CollectionUtil.isNotEmpty(smsMsgList)) {
            List<TaskInfo> taskInfoList = new ArrayList<>();
            for (Msg msg : smsMsgList) {
                SmsContentModel smsContentModel = new SmsContentModel();
                smsContentModel.setContent(msg.getContent());
                smsContentModel.setUrl(msg.getHandlerUrl());

//                Map<String, Object> map = new HashMap<>();
//                JSONObject jsonObject = JSONObject.parseObject(msg.getHandlerParams());
//                for(String key:jsonObject.keySet()){
//                    map.put(key,jsonObject.get(key));
//                }
                Map<String, Object> map = JSONObject.parseObject(msg.getHandlerParams(), Map.class);

                smsContentModel.setContentMap(map);
                smsContentModel.setTemplateId(msg.getNoteTemplateId());

                TaskInfo taskInfo = new TaskInfo();
                taskInfo.setContentModel(smsContentModel);
//                taskInfo.setMsgIds(smsMsgList.stream().map(Msg::getId).collect(Collectors.toList()));
//                taskInfo.setReceiverUserIds(smsMsgList.stream().map(Msg::getUserId).collect(Collectors.toList()));
                taskInfo.setMsgIds(Arrays.asList(msg.getId()));
                taskInfo.setReceiverUserIds(Arrays.asList(msg.getUserId()));
                taskInfo.setSendChannel(ChannelType.SMS);
                taskInfo.setMsgType(msg.getMsgType());
                taskInfo.setMobiles(smsMsgList.stream().map(Msg::getMobile).collect(Collectors.toList()));
                taskInfoList.add(taskInfo);
            }
            SendTaskModel sendTaskModel = SendTaskModel.builder()
                    .taskInfoList(taskInfoList)
                    .build();
            //责任链model
            ProcessContext context = ProcessContext.builder()
                    .code(BusinessCode.TIMED_SEND)
                    .processModel(sendTaskModel)
                    .needBreak(false)
                    .response(R.success()).build();
            //启动责任链
            ProcessContext process = processController.process(context);
        }
    }
}
